/*
 * This file is part of TradeHelper library.
 *
 * TradeHelper library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TradeHelper library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TradeHelper library.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.rag.forum.ui;

import com.alee.laf.list.WebList;
import com.rag.core.Item;
import com.rag.utils.RagUtils;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * User: mgarin Date: 05.04.12 Time: 13:12
 */

public class ItemList extends WebList
{
    private static List<ItemList> lists = new ArrayList<ItemList> ();

    public ItemList ()
    {
        super ();
        initialize ();
    }

    public ItemList ( Vector<?> listData )
    {
        super ( listData );
        initialize ();
    }

    public ItemList ( Object[] listData )
    {
        super ( listData );
        initialize ();
    }

    public ItemList ( ListModel dataModel )
    {
        super ( dataModel );
        initialize ();
    }

    private void initialize ()
    {
        lists.add ( this );

        setOpaque ( false );
        setSelectionMode ( ListSelectionModel.SINGLE_SELECTION );
        setPrototypeCellValue ( getPrototypeItem () );

        setCellRenderer ( new ItemListCellRenderer () );

        addListSelectionListener ( new ListSelectionListener ()
        {
            public void valueChanged ( ListSelectionEvent e )
            {
                ItemList list = ItemList.this;
                int index = list.getSelectedIndex ();
                if ( index != -1 )
                {
                    list.scrollRectToVisible ( list.getCellBounds ( index, index ) );
                    for ( WebList other : lists )
                    {
                        if ( other != list )
                        {
                            other.clearSelection ();
                        }
                    }
                }
            }
        } );
    }

    private static Item prototypeItem = null;

    private static Item getPrototypeItem ()
    {
        if ( prototypeItem == null )
        {
            prototypeItem = RagUtils.findBestItem ( "Jellopy" );
        }
        return prototypeItem;
    }
}